package day01;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

public class Demo03 implements Callable<Integer> {
    public Integer call() throws Exception {
        int sum=0;
        for (int i = 1; i <=100 ; i++) {
            if(i%2==0){
                sum+=i;
                Thread.sleep(300);
                System.out.println("目前累加到："+sum);
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        Demo03 d=new Demo03();//Callable类型
        FutureTask<Integer> ft=new FutureTask<>(d);
        Thread t=new Thread(ft);//只能调用Runnable
        t.start();
        Integer sum= null;
        try {
            sum = ft.get();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        System.out.println("计算结果时："+sum);
    }
}
